package com.ciwong.epaper.modules.me.ui.mall;

import android.app.Activity;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.me.bean.Service;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.DateUtils;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.MathExtend;
import com.ciwong.epaper.widget.TeacherCommentDialog;
import com.ciwong.libs.utils.CWLog;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.commons.httpclient.util.DateUtil;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by neo on 2016/7/12.
 */
public class ItemServiceBrief extends LinearLayout{

    private ImageView iv_service_logo;
    private  TextView tv_service_name;
    private  TextView price_now_sale;
    private TextView     tv_on_sale_remain_time;
    private  TextView tv_discoint_sale;
    private  TextView money_singn1_sale;
    private SpannableString msp = null;
    private  Service mService;

    public ItemServiceBrief(Context context) {
        super(context);
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_service_brief, this);
        initView( );
    }


    public ItemServiceBrief(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_service_brief, this);
        initView();

    }

    public ItemServiceBrief(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_service_brief, this);
        initView( );
    }


    private void initView( ) {

        iv_service_logo = (ImageView) findViewById(R.id.iv_service_logo);
        tv_service_name = (TextView) findViewById(R.id.tv_service_name);
        tv_on_sale_remain_time = (TextView) findViewById(R.id.tv_on_sale_remain_time);
        tv_discoint_sale = (TextView) findViewById(R.id.tv_discoint_sale);
        money_singn1_sale = (TextView) findViewById(R.id.money_singn1_sale);
        price_now_sale = (TextView) findViewById(R.id.price_now_sale);

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (null != mService) {
                    MeJumpManager.jumpBuyService((Activity) getContext(), R.string.go_back,mService.getId(),-1, EpaperConstant.REQUEST_CODE_CLASS_JUMP_BUY_SERVICE_PAGE,"");
                }

            }
        });
    }

    public  void  update(Service service)
    {

        if (service != null) {

            mService=service;
            tv_service_name.setText(service.getName());
            String nowPrice="￥"+service.getPromotion().getPrice().get(0).getOprice();
            msp = new SpannableString(nowPrice);
            //设置删除线
            msp.setSpan(new StrikethroughSpan(), 0, nowPrice.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            money_singn1_sale.setText(msp);
            String logo = service.getLogo();
            logo = TextUtils.isEmpty(logo) ? "drawable://" + R.mipmap.default_img_bg_small
                    : logo;
            ImageLoader.getInstance().displayImage(logo, iv_service_logo,
                    EConstants.getDefAvatarCircleBuilder());

            if (service.getPromotion() != null) {

                price_now_sale.setText(service.getPromotion().getPrice().get(0).getDprice()+"");
                double oPrice=new Double(service.getPromotion().getPrice().get(0).getDprice());
                double discount=0;
                if(oPrice>0) {
                    discount = Double.valueOf(service.getPromotion().getPrice().get(0).getDprice())/Double.valueOf(service.getPromotion().getPrice().get(0).getOprice());
                    discount=discount*10;
                }
                tv_discoint_sale.setText(new java.text.DecimalFormat("#.0").format(discount)+"折");


                Date endTime=DateUtils.String2Date(DateUtils.DATE_LONG,service.getPromotion().getEndTime());
                Date nowTime = new Date();
                CWLog.d("ciwong", "#########service.getPromotion().getEndTime()########"+service.getPromotion().getEndTime());
                long remainTime = endTime.getTime() - nowTime.getTime();
                String remainDay = String.format("%02d", remainTime/(24*60*60*1000));
                String remainHours = String.format("%02d", (remainTime%(24*60*60*1000)/(60*60*1000)));
                String remainMin = String.format("%02d", (remainTime%(60*60*1000))/(60*1000));
                String remainSecond = String.format("%02d", (remainTime%(60*1000))/1000);
                CWLog.d("ciwong", "#########remainDay########"+remainDay);
                CWLog.d("ciwong", "#########remainHours########"+remainHours);
                CWLog.d("ciwong", "#########remainMin########"+remainMin);
                CWLog.d("ciwong", "#########remainSecond########"+remainSecond);
                String spt=remainDay+"天";
                /*String spStr = remainDay + "天" + remainHours + "时" + remainMin + "分" ;
                msp = new SpannableString(spStr);
                msp.setSpan(new RelativeSizeSpan(1.3f), 0, remainDay.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);  //2.0f表示默认字体大小的两倍
                msp.setSpan(new RelativeSizeSpan(1.3f), remainDay.length()+1, remainDay.length()+3, Spanned.SPAN_INCLUSIVE_INCLUSIVE);  //2.0f表示默认字体大小的两倍
                msp.setSpan(new RelativeSizeSpan(1.3f), remainDay.length()+remainHours.length()+2, remainDay.length()+remainHours.length()+4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);  *///2.0f表示默认字体大小的两倍
//                msp.setSpan(new RelativeSizeSpan(1.3f), remainDay.length()+remainHours.length()+remainMin.length()+3, remainDay.length()+remainHours.length()+remainMin.length()+5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);  //2.0f表示默认字体大小的两倍
                tv_on_sale_remain_time.setText(spt);

            }
        }
    }




}
